layui.config({
    base:'/static/extra/layui/plugins/treetable-lay/dist/'
}).use(['layer', 'util', 'treeTable'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var util = layui.util;
    var treeTable = layui.treeTable;

    // 渲染表格
    var insTb = treeTable.render({
        elem: '#table',
        url: 'index',
        method:'post',
        height: 'full-150',
        size:'sm',
        tree: {
            iconIndex: 3,
            isPidData: true,
            idName: 'id',
            pidName: 'pid'
        },
        defaultToolbar: ['filter', 'print', 'exports', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [
            [
                {type: 'checkbox'}
                , {title:'操作',toolbar:'#opera',width:115}
                , {field: 'id', title: 'id', sort: true,width:50}
                , {field: 'name', title: '名称', width: 150,event: 'name'}
                , {field: 'icon', title: '图标', align:'center',width: 70,templet:"<p><i class=\"layui-icon \">{{d.icon}}</i></p>"}
                , {field: 'sort', title: '排序', event: 'sort',align:'center', width: 50, config: {type: 'input'}}
                , {field: 'flag_name', title: '所属模块', width: 150, config: {type: 'input'}}
                , {field: 'link', title: '路径', event: 'link', align:'center', width: 150,templet:function(d){
                    if(d.link == ''){
                        return '-';
                    }else{
                        return d.link;
                    }
                }}
                , {field: 'controller', title: '控制器', width: 150,event: 'controller', config: {type: 'input'}}
                , {field:'action',title:'方法',event: 'action',align:'center',templet:function(d){
                    if(d.action == ''){
                        return '-';
                    }else{
                        return d.action;
                    }
                } }
                , {field:'is_show',title:'是否显示',templet:function (item){
                    if(item.is_show == 0){
                        return "<span class=\"layui-badge\">不显示</span>";
                    }else{
                        return "<span class=\"layui-badge layui-bg-orange\">显示</span>";
                    }
                }}
                , {field: 'type', title: '类别', width: 150,templet: function(d){
                    if(d.type == 0 ){
                        return "<span class=\"layui-badge  \">菜单组</span>";
                    }else if(d.type == 1){
                        return "<span class=\"layui-badge  layui-bg-blue \">菜 单</span>";
                    }else if(d.type == 2){
                        return "<span class=\"layui-badge  layui-bg-cyan \">按 钮</span>";
                    }else{
                        return "<span class=\"layui-badge  layui-bg-gray \">未 知</span>";
                    }
                } }
                , {field: 'createtime', title: '创建时间',  width: 200, config: {type: 'date', dateType: 'date'}}
                , {field:'updatetime',title:'修改时间', width: 200}
            ]
        ],
        style: 'margin-top:0;'
    });

    // 工具列点击事件
    treeTable.on('tool(table)', function (obj) {
        var event = obj.event,
            data = obj.data;
        if (event === 'del') {
            layer.confirm('此操作将删除菜单及其他的所有子菜单，请确认',{
                btn:['确认','取消'],
            },function(index, layero){
                layer.close(index);
                $.get('delete', {id:data.id},function(r){
                    if(r.code == 1){
                        showpopup(r.msg);
                        obj.del();
                    }else{
                        showpopup(r.msg);
                    }
                },'json')
            },function(index){
                layer.close(index);
            })


        } else if (event === 'edit') {
            layer.open({
                type:2,
                content:'update?id='+data.id,
                shadeClose:true,
                area:['80%','80%'],
            })
        }
    });

    $(document).on('click','#btnExpandAll',function () {
        if(insTb.checkStatus().length == 0){
            insTb.expandAll();
        }else{
            $.each(insTb.checkStatus(),function(index,item){
                if(item.children) {
                    insTb.expand(item.id);
                }
                return true;
            })
        }
        $(this).attr('id','btnFoldAll').html('<i class="layui-icon layui-icon-spread-left">折叠');
    });

    $(document).on('click','#btnFoldAll',function () {
        $(this).attr('id','btnExpandAll').html('<i class="layui-icon layui-icon-shrink-right">展开');
        if(insTb.checkStatus().length == 0){
            insTb.foldAll();
        }else{
            $.each(insTb.checkStatus(),function(index,item){
                if(item.children){
                    insTb.fold(item.id);
                }
                return true;
            })
        }
    });

    $("#btnSearch").click(function(){
        var keywords = $('#edtSearch').val();
        if (keywords) {
            insTb.filterData(keywords);
        } else {
            insTb.clearFilter();
        }
    })

    $('#btnClearSearch').click(function(){
        insTb.clearFilter();
    })

    $("#btnDeleteChecked").click(function(){
        let ids = [];
        $.each(insTb.checkStatus(),function(index,item){
           ids.push(item.id);
        })
        $.post('delete',{ids:ids},function(r){
            if(r.code == 1){
                showpopup(r.msg,true);
            }else{
                showpopup(r.msg);
            }
        },'json');
    })
})